import React, { Component } from 'react';
import { View, Text, Alert } from 'react-native';
import { orderBaseStyle } from '../_common/style';
import { TitleCpt } from '../_components/TitleCpt';
import { FormItem } from '../../../../components/form/formItem';
import { CURRENCY } from '../../../../utils/helper';
import MethodUtil from '../../../../utils/MethodUtil';
import OrderAPIService from '../_common/OrderAPIService';

export class GoodsBlock extends Component {
	constructor(props) {
		super(props);
		let { pageParams, goodsEdit } = this.props.data || {};
		this.state = {
			pageParams: pageParams || {},
			goodsData: goodsEdit || [],
			total: 0,
			sellTotal: 0, // 闭口 销售
			buyTotal: 0,// 闭口 采购
			profit: 0, //闭口 毛利
			quantityTotal: 0  //数量
		}
		this.groupOnoff = null;
	}

	componentWillMount() {
		console.log('商品模块数据', this.props.data)
		let { pageParams, goodsData } = this.state || {};
		let { tCode } = pageParams || {};
		if (tCode == 'QH') {
			this._getOrderOtherPrice(goodsData);
		}
	}

	componentWillReceiveProps(props) {
		let { goodsEdit, pageParams } = props.data || {};
		if (goodsEdit) {
			this.setState({
				goodsData: goodsEdit,
				pageParams
			})
		} else {
			this.setState({
				pageParams
			})
		}
		goodsEdit && goodsEdit.forEach(item => {
			this.groupOnoff = item.bizTeamId;
		})
		if (goodsEdit && !goodsEdit.length) {
			this.groupOnoff = null;
		}
		console.log('this.groupOnoff', this.groupOnoff)
	}

	_moreFn = (moreData) => {
		let { index, editData } = moreData || {};
		let { navigation, goodsCallback, longFrameData, longFrameIdArr, frameGoodsList, isGoodsId, bidId } = this.props.data || {};
		longFrameData = JSON.parse(JSON.stringify(longFrameData))
		let { goodsData, pageParams } = this.state || {};
		let { tCode, companyId, companyIdBuy, isUnite, orderType, creditLevelSell, creditLevelBuy } = pageParams || {};
		if (orderType == 'S' && tCode != 'QH' && tCode != 'C') {
			if (isUnite == null) {
				Alert.alert('提示', `请先选择业务类型`, [
					{
						text: '确认', onPress: () => {
							this._removeItem(index)
						}
					}
				]);
				return
			}
		}
		//orderType == 'S' && tCode != 'QH' && tCode != 'C'

		let goodsStateList = goodsData.map(item => {
			return item.goodsState
		})
		navigation.navigate(tCode == 'QH' ? 'QHNewProductFormPage' : 'NewProductFormPage', {
			productList: goodsData,
			productIndex: index || 0,
			editData: editData || longFrameData,
			longFrameIdArr,
			frameGoodsList,
			...pageParams,
			goodsStateList,
			isGoodsId,
			bidId,
			groupOnoff: this.groupOnoff,
			creditLevelSell,
			creditLevelBuy,
			callBack: (callBackData) => {
				console.log('子组建了商品----122', callBackData)
				this.setState({
					goodsData: callBackData
				}, async () => {
					if (tCode == 'QH') {
						await this._getOrderOtherPrice(callBackData);//泉化;
					}
					goodsCallback && goodsCallback(callBackData)
				})
			}
		});
	}

	_rightFn = () => {
		let { navigation } = this.props.data || {};
		let { pageParams } = this.state;
		navigation.navigate('historyOrderList', {
			orderType: pageParams.orderType,
			tCode: pageParams.tCode,
			hisCallback: pageParams.hisCallback
		});
	}

	_removeItemOk = (index) => {
		Alert.alert('提示', `确定删除该商品吗?`, [
			{
				text: '确认', onPress: () => {
					this._removeItem(index)
				}
			},
			{
				text: '取消', onPress: () => { }
			},
		]);
	}

	//删除商品
	_removeItem = (index) => {
		let { goodsData, pageParams } = this.state;
		let { goodsCallback } = this.props.data || {};
		let { tCode } = pageParams || {};
		goodsData.splice(index, 1);
		this.setState({
			goodsData
		}, () => {
			if (tCode == 'QH' && goodsData.length) {
				this._getOrderOtherPrice(goodsData);//泉化;
			}
			goodsCallback && goodsCallback(goodsData)
		})
	}


	//泉化 现货价获取
	_getOrderOtherPrice = async (qhData) => {

		//let { goodsData } = this.state;
		let { longFrameData, longFrameIdArr } = this.props.data || {};
		let { itemId, goodsId, isQhFlag, sourceData } = qhData[0] || {};
		// 液化气 不计算现货价

		let subData = {
			orderId: longFrameIdArr,
			orderItemInfo: {
				orderId: longFrameIdArr,
				itemId,
				goodsId,
				priceType: qhData[0].priceType,
				longOrderId: longFrameIdArr
			}
		}
		console.log('longFrameIdArr', longFrameIdArr, 'goodsId', goodsId)
		if (!longFrameIdArr || !goodsId) return

		let respData = await OrderAPIService.getOrderOtherPrice(subData) || {};
		console.log('外面展示现货价-respData', respData);
		if (qhData[0].sourceData == 'OFFLINE') {
			qhData[0].suggestedPrice = respData.suggestedPrice || 0;  // 现货价
		}
		this.setState({ goodsData: qhData })
	}

	render() {
		let { goodsData, pageParams } = this.state || [];
		let { currency, tCode } = pageParams || {};
		let titleQH = '新增商品';
		let showAddBtn = true; // 默认显示新增商品按钮 
		let { bidId } = this.props.data || {};
		// 如果已经选择了一种商品并且是液体的，隐藏新增按钮
		if (goodsData && goodsData.length == 1 && (goodsData[0].goodsState == 'FLUID' || goodsData[0].goodsState == 'GAS')) showAddBtn = false

		if (tCode == 'QH' && goodsData.length >= 1 || !showAddBtn) {
			titleQH = null;
		}
		return (
			<View style={orderBaseStyle.box}>
				<TitleCpt
					data={{
						title: '商品信息',
						moreText: titleQH,
						rightText: tCode == 'E' || tCode == 'QH' || bidId ? null : '[一键下单]',
						_moreFn: () => { this._moreFn({ index: goodsData.length }) },
						_rightFn: tCode == 'E' || tCode == 'QH' ? null : this._rightFn
					}} />
				{
					goodsData && goodsData.length > 0 ? (
						goodsData.map((item, index) => {
							return (
								<View style={[orderBaseStyle.goodsBox, index < goodsData.length ? orderBaseStyle.goodsXian : null]} key={index}>
									<FormItem labelStyle={orderBaseStyle.goodsLabel} handleCallBack={() => { this._moreFn({ index, editData: item }) }} type="select" label={item.goodsName} />
									<View style={orderBaseStyle.goodsRow}>
										<View style={orderBaseStyle.goodsZhi}>
											<Text style={[tCode == 'C' ? orderBaseStyle.goodsTextC : null]}><Text style={orderBaseStyle.goodsBold}>{item.quantity}</Text>吨</Text>
											{tCode == 'C' ? <View style={[orderBaseStyle.goodsIconBox, orderBaseStyle.goodsBuy]}><Text style={[orderBaseStyle.goodsIcon]}>采</Text></View> : null}
											{tCode != 'C' ? <Text style={orderBaseStyle.gun}>|</Text> : null}
											<Text style={orderBaseStyle.goodsText}><Text style={orderBaseStyle.goodsBold}>{tCode == 'C' ? item.purchasePrice : item.price}</Text>{CURRENCY.getUnit(currency).text}/吨</Text>
											{
												item.priceType == '1' ? <Text style={orderBaseStyle.goodsPrice}>一口价</Text> : <Text style={orderBaseStyle.goodsPrice2}>暂定价</Text>
											}
										</View>
										{tCode != 'C' ? <Text style={orderBaseStyle.goodsRemove} onPress={() => { this._removeItemOk(index) }}>删除</Text> : null}
									</View>

									{
										tCode == 'C' ? (
											<View style={[orderBaseStyle.goodsRow, orderBaseStyle.goodsRowActive]} >
												<View style={orderBaseStyle.goodsZhi}>
													<Text style={orderBaseStyle.goodsTextC}></Text>
													<View style={[orderBaseStyle.goodsIconBox, orderBaseStyle.goodsSell]}><Text style={[orderBaseStyle.goodsIcon]}>销</Text></View>
													<Text style={orderBaseStyle.goodsText}><Text style={orderBaseStyle.goodsBold}>{item.salePrice}</Text>{CURRENCY.getUnit(currency).text}/吨</Text>
													{
														item.priceType == '1' ? <Text style={orderBaseStyle.goodsPrice}>一口价</Text> : <Text style={orderBaseStyle.goodsPrice2}>暂定价</Text>
													}
												</View>
												<Text style={orderBaseStyle.goodsRemove} onPress={() => { this._removeItemOk(index) }}>删除</Text>
											</View>
										) : null
									}

								</View>
							)
						})
					) : <Text onPress={() => { this._moreFn({ index: goodsData.length }) }} style={orderBaseStyle.notgood}>请新增交易商品...</Text>
				}
			</View>
		)
	}
}

